package me.youline.dataServer.service.support.file;

import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import me.youline.dataServer.service.support.config.AvatarImageSizeConfig;
import me.youline.dataServer.service.support.file.AvatarFileLocator.AvatarFileLocationConfig;
import me.youline.dataServer.service.support.file.FileBuilder.FileEntity;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 头像文件定位器
 * @author linhan
 *
 */
@Component
public class AvatarFileLocator extends AbstractImgFileLocator<AvatarImageSizeConfig> implements NewFileLocatable<AvatarFileLocationConfig>{
	
	@Value("#{frontProperties['resource.fileCategory.avatar']}")
	private String categoryName = "avatar";
	
	@Autowired
	public AvatarFileLocator(AvatarImageSizeConfig config) {
		super(config);
	}

	@Override
	public FileEntity getNewFileEntity(AvatarFileLocationConfig avatarConfig) {
		Date d = DateUtils.ceiling(new Date(), Calendar.DATE);
		return FileBuilder.newInstance(avatarConfig.getInput()).root(this.getFileRoot())
				.dir(String.valueOf(d.getTime())).dir(String.valueOf(avatarConfig.getUserId())).file(String.valueOf(new Random().nextInt(2000))).getEntity();
	}
	
	@Override
	protected String getFileCategoryName() {
		return categoryName;
	}
	
	

	/**
	 * 头像文件定位配置
	 * @author linhan
	 *
	 */
	public static class AvatarFileLocationConfig extends FileLocationConfig {

		private final Long userId;

		public AvatarFileLocationConfig(InputStream input, Long userId) {
			super(input,0L);
			this.userId = userId;
		}
		
		public Long getUserId() {
			return userId;
		}
	}

}
